﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace Jurassic_Island
{
    public class CADObjetoMochila
    {
        static private String cadenaConexion = ConfigurationManager.ConnectionStrings["JurassicBDConnectionString"].ConnectionString;
        static private String nombreTabla = "Objetos_Mochila";

        public DataSet ObtenerTablaObjetosMochila()
        {
            DataSet dsObjMochila = new DataSet();

            try
            {
                SqlConnection conexion = new SqlConnection(cadenaConexion);
                String consulta = "Select * from " + nombreTabla;
                SqlDataAdapter daObjMochila = new SqlDataAdapter(consulta, conexion);
                daObjMochila.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                daObjMochila.Fill(dsObjMochila, nombreTabla);
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return dsObjMochila;
        }

        public DataSet ObtenerTablaObjMochilaPorID(int id_objmoch)
        {
            DataSet dsObjMochila = new DataSet();

            try
            {
                SqlConnection conexion = new SqlConnection(cadenaConexion);
                String consulta = "Select * from " + nombreTabla + " where id_objmoch=" + id_objmoch;
                SqlDataAdapter daObjMochila = new SqlDataAdapter(consulta, conexion);
                daObjMochila.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                daObjMochila.Fill(dsObjMochila, nombreTabla);
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return dsObjMochila;
        }

        public DataSet ObtenerTablaObjMochilaPorPartida(int partida_objmoch)
        {
            DataSet dsObjMochila = new DataSet();

            try
            {
                SqlConnection conexion = new SqlConnection(cadenaConexion);
                String consulta = "Select * from " + nombreTabla + " where partida_objmoch='" + partida_objmoch.ToString() + "'";
                SqlDataAdapter daObjMochila = new SqlDataAdapter(consulta, conexion);
                daObjMochila.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                daObjMochila.Fill(dsObjMochila, nombreTabla);
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return dsObjMochila;
        }

        public bool ActualizarObjetosMochila(DataSet dsObjMochila)
        {
            bool resultado = false;

            try
            {
                SqlConnection conexion = new SqlConnection(cadenaConexion);
                String consulta = "Select * from " + nombreTabla;
                SqlDataAdapter daObjMochila = new SqlDataAdapter(consulta, conexion);
                SqlCommandBuilder cb = new SqlCommandBuilder(daObjMochila);
                daObjMochila.Update(dsObjMochila, nombreTabla);
                dsObjMochila.Tables[nombreTabla].AcceptChanges();

                resultado = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return resultado;
        }
    }
}
